package cjgl.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cjgl.entity.Student;
import cjgl.entity.TbClass;
import cjgl.service.StudentService;
import cjgl.service.TbClassService;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TbClassService tbClassService = new TbClassService();
	private StudentService studentService = new StudentService();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("table".equals(method)){
			table(request,response);
		}else if("update".equals(method)) {
			update(request,response);
		}else if("save".equals(method)) {
			save(request,response);
		}else if("delete".equals(method)) {
			delete(request,response);
		}else if("add".equals(method)) {
			add(request,response);
		}
	}
	
	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String bjIdStr = request.getParameter("bjId");
		Integer bjId = Integer.parseInt(bjIdStr);
		Student model = new Student();
		model.setTbclass(new TbClass(bjId));
		request.setAttribute("model",model);
		request.getRequestDispatcher("jsp/xuesheng/edit.jsp").forward(request, response);
	}
	

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		Integer bjId = studentService.findById(id).getTbclass().getId();
		studentService.delete(id);
		response.sendRedirect("mian?page=4&bjId="+bjId);
	}
	
	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		String name = request.getParameter("name");
		String stuNo = request.getParameter("stuNo");
		String password = request.getParameter("password");
		String bjIdStr = request.getParameter("tbClassid");
		Integer bjId= Integer.parseInt(bjIdStr);
		Integer id=null;
		if(idStr!=null&&!"".equals(idStr)) {
			id=Integer.parseInt(idStr);
		}
		Student tb= new Student(id,name, password,stuNo,bjId);
		studentService.save(tb);
		response.sendRedirect("mian?page=4&bjId="+bjId);
		
	}
	
	
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		Student model = studentService.findById(id);
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/xuesheng/edit.jsp").forward(request, response);
		
	}



	private void table(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("bjId");
		Integer id=Integer.parseInt(idStr);
		List<Student> list = studentService.findByTbClassId(id);
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/xuesheng/table.jsp").forward(request, response);
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId=null;
		String bjIdStr = request.getParameter("bjId");
		if(bjIdStr!=null&&!"".equals(bjIdStr)) {
			bjId=Integer.parseInt(bjIdStr);
		}
		
		List<TbClass> bjs = tbClassService.findAll();
		request.setAttribute("bjId", bjId);
		request.setAttribute("bjs", bjs);
		if(bjId!=null) {
			request.setAttribute("student",studentService.findByTbClassId(bjId));
		}
		request.getRequestDispatcher("jsp/xuesheng/list.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
